package com.qianclass.talklive.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qianclass.talklive.R;
import com.qianclass.talklive.app.bean.TranslationBean;

import java.util.List;

/**
 * Author:  Administrator
 * Date:    2017/3/21     14:34
 * TODO:    
 */

public class NetParaphraseAdapter extends BaseAdapter {

    private Context mContext;
    private TranslationBean mTranslationBean;
    private String mParaphraseType;

    public NetParaphraseAdapter(Context context, TranslationBean translationBean, String paraphraseType) {
        mContext = context;
        mTranslationBean = translationBean;
        mParaphraseType = paraphraseType;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (mParaphraseType.equals("basic")) {
            List<String> explains = mTranslationBean.getBasic()
                                                    .getExplains();
            count = explains != null ? explains.size() : 0;
        } else if (mParaphraseType.equals("web")) {
            List<TranslationBean.WebBean> webBeanList = mTranslationBean.getWeb();
            count = webBeanList != null ? webBeanList.size() : 0;
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_translation_web, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (mParaphraseType.equals("basic")) {
            holder.tvKey.setVisibility(View.GONE);

            List<String> explains = mTranslationBean.getBasic()
                                                    .getExplains();

            holder.tvValue.setText(explains.get(position));
            holder.tvValue.setTextColor(Color.BLACK);
        } else if (mParaphraseType.equals("web")) {
            TranslationBean.WebBean webBean = mTranslationBean.getWeb().get(position);
            holder.tvKey.setText(webBean.getKey());

            List<String> value = webBean.getValue();
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < value.size(); i++) {
                if (i == value.size() - 1) {
                    buffer.append(value.get(i));
                } else {
                    buffer.append(value.get(i) + ";");
                }
            }
            holder.tvValue.setText(buffer.toString());
        }

        return convertView;
    }

    class ViewHolder{
        TextView tvKey;
        TextView tvValue;

        public ViewHolder(View root){
            tvKey = (TextView) root.findViewById(R.id.tv_key);
            tvValue = (TextView) root.findViewById(R.id.tv_value);
        }
    }
}
